<?php

namespace App\Console\Commands;

use App\Models\Order;
use Illuminate\Console\Command;

class CancelOrderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cancel:order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'auto cancel orders';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders = Order::query()
            ->status('pay')
            ->where('cancel_at', '<>', 0)
            ->where('cancel_at', '<=', now()->timestamp)
            ->get();

        $orders->map(function (Order $order) {
            // 钱包的钱退款
            $order->refund(Order::PAYMENT_SELF);
            // 状态改变
            $order->getHint()->cancel('未及时付款, 自动取消');
        });
    }
}
